﻿<%@ page Language="C#" AutoEventWireup="false" Inherits="KPIReport.Web.Lists.Administration.UserManagement.sRoleUserlist" CodeFile="sRoleUserlist.aspx.cs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server" id="Head1">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.fmatter.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/helper.js") %>" ></script>

    <br />
    <table id="tblUser" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pgrUser" class="scroll">
    </div>
    <br />

    <button id="btnSave" type="button">Simpan</button>
    <button id="btnClose" type="button">Tutup</button>

    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });
 
    </script>
    <script type="text/javascript">

        var roleId = '<%= Request.Params["roleId"] %>';
        
        var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserhandler.ashx") %>';

        var idsOfSelectedRows = [],
        updateIdsOfSelectedRows = function (id, isSelected) {
            var UserID = $('#tblUser').jqGrid('getCell', id, 'UserID');
            var index = $.inArray(UserID, idsOfSelectedRows);

            if (!isSelected && index >= 0) {
                idsOfSelectedRows.splice(index, 1); // remove id from the list
            }
            else if (index < 0) {
                idsOfSelectedRows.push(UserID);
            }
        };

        $('#tblUser').jqGrid({
            url: handlerUrl + '?method=getdata',
            datatype: 'json',
            colNames: ['User ID', 'User Name'],
            colModel: [
                { name: 'UserID', hidden: true, index: 'USERID', width: 60 },
           		{ name: 'UserName', index: 'USERNAME', width: 383 }
           	],
            rowNum: 10,
            id: 'UserID',
            multiselect: true,
            rowList: [5, 10, 15, 20],
            pager: '#pgrUser',
            sortname: 'USERNAME',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 500,
            height: '100%',
            caption: 'User',
            gridview: true,
            rownumbers: true,
            ajaxGridOptions: { cache: false },
            onSelectRow: updateIdsOfSelectedRows,
            onSelectAll: function (rowsId, isSelected) {

                for (var i = 0; i < rowsId.length; i++) {
                    var id = rowsId[i];
                    updateIdsOfSelectedRows(id, isSelected);
                }

            },
            loadComplete: function (data) {

                $.ajax({
                    type: 'POST',
                    url: '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserRolehandler.ashx") %>' + '?method=getdatabyroleid&roleId=' + roleId,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (rows) {

                        $.each(rows, function (r, row) {
                            for (var i = 0; i < data.rows.length; i++) {
                                if (row.UserID == data.rows[i].UserID) {
                                    var idx = i + 1;
                                    updateIdsOfSelectedRows(idx, true);
                                }
                            }
                        });

                        for (var i = 0; i < data.rows.length; i++) {
                            for (var x = 0; x < idsOfSelectedRows.length; x++) {

                                if (data.rows[i].UserID == idsOfSelectedRows[x]) {
                                    var idx = i + 1;
                                    $('#tblUser').jqGrid('setSelection', idx);
                                }
                            }
                        }

                    },
                    error: function () {
                        alert("Failed to load Role");
                    }
                });

            }
        });

        function ClickSave() {

                var userIds = '';
                for (i = 0; i < idsOfSelectedRows.length; i++) {
                    if (userIds == '')
                        userIds = idsOfSelectedRows[i];
                    else
                        userIds += ',' + idsOfSelectedRows[i];

                }

                var objUser = new Object();
                objUser.UserID = encodeURIComponent(userIds);
                objUser.RoleID = encodeURIComponent(roleId);

                jsonData = JSON.stringify(objUser);

                CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserRolehandler.ashx") %>', "?method=insert", jsonData);

                parent.$('#dialog').dialog('close');

            }


            $('#btnSave').click(function () {

                ClickSave();
                return false;
            });


            $('#btnClose').click(function () {
                parent.$('#dialog').dialog('close');

                return false;
            });
    
    </script>
    </form>
</body>
</html>
